package adv46.chat;

import java.io.*;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
public class FileSendThread2 extends Thread {
   final static String FOLDER="E:\\java_workspace\\hbuilderproj\\java81\\java81\\images";
    private final Socket s;

    public FileSendThread2(Socket s){
       this.s=s;
   }

    @Override
    public void run() {
        System.out.println("准备发文件给："+s);
        try {
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            //读取你要的文件名
            BufferedReader br=new BufferedReader(new InputStreamReader(in));
            String fileName=br.readLine();
            FileInputStream fin=new FileInputStream(new File(FOLDER,fileName));
            byte[]b=new byte[1024];
            int len=0;
            while ((len=fin.read(b))!=-1){
                out.write(b,0,len);
            }
            out.flush();
            fin.close();
            System.out.println("文件发送给："+s+"成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
